﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using DG.Tweening;

public partial class BattleCtr: XObjBase
{
  
    private Dictionary<string, UIBattlePlayer> _userId2UIBattlePlayer;
    private void OnInitRender()
    {
        _userId2UIBattlePlayer = new Dictionary<string, UIBattlePlayer>();
    }


    private IEnumerator CoWaitUIBattle()
    {
        yield return new WaitUntil(() => { return UIBattle.Instance != null; });
        UIBattle.Instance.Bind(_dataBattleMgr);
        int playerCount = _dataBattleMgr.GetDataPlayerCount();
        for (int i = 0; i < playerCount; i++)
        {
            var dataPlayer = _dataBattleMgr.GetDataPlayer(i);
            int index = i;
            XFactory.Instance.CreateInstanceAsync("UIBattlePlayer", null, (vGameObject) =>
            {
                int showIndex = _dataBattleMgr.GetShowIndex(index);
                //vGameObject.transform.SetParent(UIBattle.Instance.playerRoots[showIndex].transform, false);
                var cmp = vGameObject.GetComponent<UIBattlePlayer>();
                cmp.Bind(dataPlayer);

                //决定挂在哪个点上
                cmp.SetShowIndex(showIndex);

                //缓存
                SetUIBattlePlayer(dataPlayer.GetUserId(), cmp);
            });
        }
    }

    public void SetUIBattlePlayer(string userId, UIBattlePlayer vUIBattlePlayer)
    {
        _userId2UIBattlePlayer[userId] = vUIBattlePlayer;
    }

    public UIBattlePlayer GetUIBattlePlayer(string userId)
    {
        UIBattlePlayer uiBattlePlayer;
        _userId2UIBattlePlayer.TryGetValue(userId, out uiBattlePlayer);
        return uiBattlePlayer;
    }

    //显示手指，统计骰子
    public void ShowAction(long actionType, string fromUserId, string toUserId)
    {
       var fromUIBattlePlayer = GetUIBattlePlayer(fromUserId);
       var toUIBattlePlayer = GetUIBattlePlayer(toUserId);
       var fingerTran = fromUIBattlePlayer.playerHand.transform;
       var toUserLocalPosition = fingerTran.InverseTransformPoint(toUIBattlePlayer.transform.position);
    
        toUserLocalPosition.x += 15;//加个偏移值

        Sequence quence = DOTween.Sequence();
        //去的动作
        quence.Append(fingerTran.DOLocalMove(toUserLocalPosition, 0.1f));

        //中间操作的动作
        switch (actionType)
        {
            case ActionType.Close:
                var closePos = new Vector3(toUserLocalPosition.x, toUserLocalPosition.y - 30, toUserLocalPosition.z);
                quence.Append(fingerTran.DOLocalMove(closePos, 0.4f));
                break;
            case ActionType.Shake:
                var leftPos = new Vector3(toUserLocalPosition.x - 50, toUserLocalPosition.y, toUserLocalPosition.z);
                var rightPos = new Vector3(toUserLocalPosition.x + 50, toUserLocalPosition.y, toUserLocalPosition.z);
                var centerPos = new Vector3(toUserLocalPosition.x, toUserLocalPosition.y, toUserLocalPosition.z);
                for (int i = 0; i < 4; i++)
                {
                    quence.Append(fingerTran.DOLocalMove(leftPos, 0.1f));
                    quence.Append(fingerTran.DOLocalMove(rightPos, 0.2f));
                    quence.Append(fingerTran.DOLocalMove(rightPos, 0.1f));
                }
                break;
            case ActionType.Open:
                var openPos = new Vector3(toUserLocalPosition.x - 30, toUserLocalPosition.y + 30, toUserLocalPosition.z);
                quence.Append(fingerTran.DOLocalMove(openPos, 0.4f));
                break;
            case ActionType.Glance:
                var glancePos = new Vector3(toUserLocalPosition.x, toUserLocalPosition.y + 10, toUserLocalPosition.z);
                quence.Append(fingerTran.DOLocalMove(glancePos, 0.4f));
                break;
            case ActionType.LookByMySelf:
                var lookPos = new Vector3(toUserLocalPosition.x, toUserLocalPosition.y + 5, toUserLocalPosition.z);
                quence.Append(fingerTran.DOLocalMove(lookPos, 0.4f));
                break;
            default:
                Debug.LogError("can not handle type:" + actionType);
                break;
        }

        //回的动作
        quence.Append(fingerTran.DOLocalMove(Vector3.zero, 0.9f).SetEase(Ease.InSine));

        Facade.Instance.SendEvent(EventID.BattlePlayerStateChange, null);
    }
}